<template>
  <!--  <common-edit-dialog :title="dialogTitle" :visible.sync="dialogShow" width="800px" append-to-body>-->
  <common-edit-dialog
    :visible="dialogShow"
    :title="dialogTitle"
    :button-loading="loading"
    width="800px"
    @cancel="close('onlyClose')"
    @submit="submitForm"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.native.prevent
    >
      <el-row :gutter="18">
        <el-col :span="12">
          <el-form-item label="分类名称" prop="typeName">
            <common-input
              v-model="form.orgName"
              :verify="false"
              :type="'text'"
              :maxlength="16"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <common-input-number
              :max-length="4"
              v-model="form.orderNum"
              controls-position="right"
            ></common-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <common-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8 }"
              v-model="form.remark"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </common-edit-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { sortRule } from "@/utils/formRules";
import { add, edit } from '@/api/apisecurity/applicationType/applicationType'
import { keys } from "zrender/lib/core/util";

export default {
  components: { Treeselect },
  props: {

  },
  data() {
    return {
      dialogTitle: "新增", // 标题
      dialogShow: false, // 是否显示
      form: {
        orgId: null,
        orgName: null,
        orderNum: 1,
        remark: null,
        parentId: null,
        status: 0,
        orgFullName: null,
        ancestors: null,
        appId: null,
        type: 0,
      },
      rules: {
        orgName: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur",
          },
        ],
        orderNum: sortRule,
      },
      loading: false,
    };
  },
  // watch: {
  //   dialogShow(val, oldVal) {
  //     if (!val) {
  //       this.$refs.form.resetFields();
  //       this.$emit("refresh");
  //     }
  //   },
  // },
  computed: {},
  methods: {
    /**
     * 关闭
     */
    close(onlyClose) {
      this.form = this.$options.data().form;
      this.dialogShow = false;
      // if (!onlyClose) {
      //   this.$emit("refresh");
      // }
    },
    /**
     * 显示
     */
    show(data) {
      this.dialogShow = true;
      if(data.editType === 'edit') {
        this.dialogTitle = "编辑分类"
        if(data && data.orgId) {
          Reflect.ownKeys(this.form).forEach(k => this.form[k] = data[k])
        } else {
          this.form = this.$options.data().form;
        }
      } else {
        this.dialogTitle = "新增分类"
        this.form.parentId = data && data.orgId ? data.orgId : ''
      }
      this.form.ancestors = data && data.fullPath ? data.fullPath.join(',') : ''
    },
    /**
     * 提交
     */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (!!this.form.orgId) {
            edit(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.close();
                  this.$emit("refresh");
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg).then();
                }
              })
              .finally(() => (this.loading = false));
          } else {
            add(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("新增成功");
                  this.close();
                  this.$emit("refresh");
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg).then();
                }
              })
              .finally(() => (this.loading = false));
          }
        }
      });
    },
  },
  mounted() {},
};
</script>
<style lang="scss" scoped></style>
